package design.patterns.factorymode.factory;

import common.util.FileUrlReader;
import org.springframework.beans.factory.annotation.Value;

import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Description :properties配置文件读取
 * @author: cww
 * @DateTime: 2018-07-03 22:50
 */

public class PropertiesReader {
    @Value("${properties.path}")
    private String propertiesPath;
    @Value("${mvn.jdbc.url}")
    private String url;
    public Map<String, String> getProperties() throws IOException {
        System.out.println(propertiesPath);
        System.out.println(url);
        Properties props = new Properties();
        Map<String, String> map = new HashMap<String, String>();
        InputStream inputStream = null;
        try {
            FileUrlReader fileUrlReader = new FileUrlReader();
            String url = fileUrlReader.getFileUrlFromClassPath("file/factory.properties");
            inputStream = new BufferedInputStream(new FileInputStream(url));
            InputStreamReader reader = new InputStreamReader(inputStream,"UTF-8");
            props.load(reader);
            Enumeration en = props.propertyNames();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                String property = props.getProperty(key);
                map.put(key, property);
				System.out.println(key + "  " + property);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            inputStream.close();
        }
        return map;
    }
}
